import Vue from 'vue'
import VueRouter from 'vue-router'

const modulesFiles = require.context(`./modules`, true, /\.js$/);
const modules = modulesFiles.keys().reduce((modules, modulePath) => {
  // set './app.js' => 'app'
  const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1')
  const value = modulesFiles(modulePath)
  modules[moduleName] = value.default
  return modules
}, {})

Vue.use(VueRouter)
let routes = []

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
for (let a in modules) {
  routes.push(...modules[a])
}

console.log('routerModules', modules)
const router = new VueRouter({
  // mode: 'hash',
  mode: 'history',
  routes,
  scrollBehavior (to, from) {
    // 路由切换，简单的让页面滚动到顶部
   return { x: 0, y: 0 }
  }
})


export default router
